`
https://leetcode.cn/problems/longest-non-decreasing-subarray-after-replacing-at-most-one-element/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var longestSubarray = function (nums) {
  const n = nums.length
  if (n <= 1) return 1
  // dp[i][0/1] 表示以 nums[i] 结尾的最长非递减子数组，0/1 表示可以更改的次数
  const dp = Array.from({ length: n }, () => [0, 0])
  dp[0] = [1, 1]
  let res = 1

  for (let i = 1; i < n; i++) {
    if (nums[i] >= nums[i - 1]) {
      dp[i][0] = dp[i - 1][0] + 1
      dp[i][1] = dp[i - 1][1] + 1
    } else {
      dp[i][0] = 1
    }

    if (i >= 2 && nums[i] >= nums[i - 2]) {
      dp[i][1] = Math.max(dp[i][1], dp[i - 2][0] + 2)
    } else {
      dp[i][1] = Math.max(dp[i][1], 2)
    }

    res = Math.max(res, dp[i - 1][0] + 1, dp[i][1])
  }

  return res
};